package com.woniu.controller;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.woniu.dao.GoodsMapper;
import com.woniu.dto.*;
import com.woniu.pojo.*;
import com.woniu.pojo.*;
import com.woniu.server.GoodsServer;
import com.woniu.server.impl.GoodServerImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@CrossOrigin(allowCredentials="true",maxAge = 3600)
public class GoodsController {

    @Autowired
    GoodsMapper goodsMapper;

        //查询所有商品
        @RequestMapping("/list")
        @ResponseBody
        public Map findall(){
            System.out.println("进来了·······");

           List<Vendor> goods= goodsMapper.findAll();


          // int num=goodsMapper.findConNum();


            System.out.println("这是返回过来的:"+goods.toString());

            Map map=new HashMap();
            map.put("goods",goods);


            System.out.println("结束了");
            return map;
        }

        //商品详细
        @RequestMapping("/xiangxi")
        @ResponseBody
        public Map findById(int id){

           /* System.out.println("进来了！！！！！！！！！！！！");
            FindGoods findGoods= goodServerImpl.findOne(id);

            System.out.println("看看有没有数据出来"+findGoods);

            List<RestCom> goodsComments=goodServerImpl.findCom(id);

            System.out.println("看看评论查出来没有："+goodsComments);
            Map map=new HashMap();

            map.put("comments",goodsComments);
            map.put("findGoods",findGoods);*/

            Map map=new HashMap();

            Restfind restfind=goodsMapper.findrestByVid(id);
            //查询所有的对应的子评论
            List<RestCom> goodsComments=goodsMapper.findCom(id);

           /* for(int i=0;i<goodsComments.size();i++){

               long  shu=goodsComments.get(i).getId();
                    goodsMapper.findzicom(id,shu);

            }*/
            int num=goodsMapper.findConNum(id);

            map.put("findGoods",restfind);


            map.put("coments",goodsComments);

            map.put("num",num);
            return map;
        }

        //发布物品的信息
        @RequestMapping("/addshop")
        @ResponseBody
        public  Map  add(Addgoods goods){

            System.out.println(goods.getVendorId());
            Goods num=new Goods();

            Date currentTime=new Date();
            System.out.println(currentTime);

            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String newTime=format.format(currentTime);

            try {
                Date currentTime_2 = format.parse(newTime);
                goods.setCreateTime(currentTime);
                goods.setUpdateTime(currentTime_2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //添加商品 返回id值
            goodsMapper.addshop(goods);


            //根据id值 添加图片到数据库
            //获得图片数据进行遍历

            String str =goods.getGoodsImg();
            String[] strarr = str.split(",");

            for (int i=0;i<strarr.length;i++){
                goodsMapper.addphoto(strarr[i],goods);
            }

            Map map=new HashMap();
            map.put("code",200);
            map.put("message","添加成功");

            System.out.println(goods);
            System.out.println("流程走完啊············");
            return map;
        }

        @RequestMapping("upload")
        @ResponseBody
        //图片的上传
        public Map uploadLecturer(MultipartFile file, HttpServletRequest request, Model model) throws IllegalStateException, IOException {

            String path = request.getServletContext().getRealPath("/");

            File upimgs = new File(path);
            if(!upimgs.exists()){
                upimgs.mkdirs();
            }
            String newFilePath = upimgs.getAbsolutePath()+"\\"+ UUID.randomUUID().toString()+".png";
            String path1= newFilePath.substring(newFilePath.lastIndexOf("\\")+1);
            System.out.println(newFilePath);
            File f = new File(newFilePath);
            file.transferTo(f);

            Map<String,String> code=new HashMap<String,String>();
            code.put("code","200");
            code.put("path", path1);
            return  code;

        }

        //关于评论
        @RequestMapping("")
        @ResponseBody
        public  Map comm(){

            Map map=new HashMap();

            return map;
        }

        //点击商品 商品的浏览数加1
        @RequestMapping("/addnum")
        @CrossOrigin(allowCredentials="true",maxAge = 3600)
        @ResponseBody
        public  Map add(int id){
            Map map =new HashMap();
            int likenum = goodsMapper.findlikenum(id);
             likenum++;
            goodsMapper.updatalikenum(id,likenum);
             map.put("code",200);
             return map;
        }

        //收藏商品
        @RequestMapping("addsc")
        @CrossOrigin(allowCredentials="true",maxAge = 3600)
        @ResponseBody
        public Map shoucang(int id){

            Map map=new HashMap();
            GoodsCollect goodsCollect=goodsMapper.findbygoodsidanduserid(id,1);
            if (goodsCollect==null){
                GoodsCollect goodsCollect1=new GoodsCollect();

                long id1=id;
                Date currentTime=new Date();
                System.out.println(currentTime);

                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String newTime=format.format(currentTime);

                try {
                    Date currentTime_2 = format.parse(newTime);
                    goodsCollect1.setId(id1);
                    goodsCollect1.setCollectTime(currentTime_2);

                    int a=1;
                    long a2=a;
                    goodsCollect1.setUserId(a2);

                    goodsCollect1.setCollectId(id1);

                    System.out.println("看看新增的对象："+goodsCollect1);
                    goodsMapper.addcollect(goodsCollect1);

                    map.put("code",200);

                    return map;
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            if (goodsCollect!=null){

                long collectid= goodsCollect.getId();
                goodsMapper.deletecollectbyid(collectid);

                map.put("code","201");

                return map;

            }


            return  map;



        }

        //根据类型进行查看
        @RequestMapping("/listfenlei")
        @CrossOrigin(allowCredentials="true",maxAge = 3600)
        @ResponseBody
        public  Map listfe(String leixing){


            System.out.println("进来了 哈哈哈："+leixing);
            Map map =new HashMap();


       List<good> goods = goodsMapper.findbyleixing(leixing);
           System.out.println(goods);

           map.put("goods",goods);

            System.out.println(map);
            return map;

        }
        //查询自己吓的订单
    @RequestMapping("/findorder")
    @CrossOrigin(allowCredentials="true",maxAge = 3600)
    @ResponseBody
    public Map finord(int id){
            Map map =new HashMap();

           // goodsMapper.findorder(id);
      Orderss orderss= goodsMapper.findorder(1);

        map.put("orders",orderss);
            return map;


    }

}
